package com.ceuer.demo.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 创建线程方式3
 * 1.实现Callable接口,实现call()方法
 * 2.Callable的好处：
 *  a.可以定义返回值(本例是返回Boolean);
 *  b.可以抛出异常
 */
public class Demo3 implements Callable<Boolean> {
	private String url;
	private String name;
	
	public Demo3(String url,String name) {
		this.url = url;
		this.name = name;
	}
	
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		Demo3 d1=new Demo3("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "7.png");
		Demo3 d2=new Demo3("https://www.baidu.com/img/flexible/logo/pc/peak-result.png", "8.png");
		Demo3 d3=new Demo3("https://www.baidu.com/img/flexible/logo/pc/result.png", "9.png");
		
		//part1 创建执行服务
		ExecutorService service= Executors.newFixedThreadPool(3);
		
		//part2 提交执行任务
		Future<Boolean> f1=service.submit(d1);
		Future<Boolean> f2=service.submit(d2);
		Future<Boolean> f3=service.submit(d3);
		
		//part3 获取执行结果
		boolean rs1=f1.get();
		boolean rs2=f2.get();
		boolean rs3=f3.get();
		
		//part4 关闭执行服务
		service.shutdown();
	}
	
	@Override
	public Boolean call() throws Exception {
		WebDownLoader webDownLoader=new WebDownLoader();
		webDownLoader.downloader(url,name);
		System.out.println("下载了文件名为--->"+name);
		return true;
	}
}